package com.doctor.windflower_doctor.fragment;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.QuestionAction;
import com.doctor.windflower_doctor.adapter.QuestionStateAdapter;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.entity.QuestionBeen;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.util.StringUtil;
import com.doctor.windflower_doctor.view.LoadingDialog;
import com.doctor.windflower_doctor.view.PullToCustomListView;
import com.doctor.windflower_doctor.view.PullToCustomListView.OnAutoLoadBottomListener;
import com.doctor.windflower_doctor.view.PullToRefreshBase.OnRefreshListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;

/**
 * <p>
 * 功能
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年10月11日 下午3:07:45
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class QuestionArrivingFragment extends BaseFragment implements Constent {
	private static QuestionArrivingFragment baseFragment;
	private QuestionStateAdapter questionListAdapter;
	private PullToCustomListView custom_list;
	private ListView toBeSolvedList;
	private LoadingDialog loadingDialog;
	private Button left_btn, center_btn, right_btn;
	private ImageView center_red, right_red;
	private SharedPreferencesDB sharedPreferencesDB;
	private String id = "";
	private Resources resources;
	private RelativeLayout question_bar;
	private TextView textView1, textView2, tv_answer;

	public static QuestionArrivingFragment getInstance(Bundle bundle) {
		if (baseFragment == null) {
			baseFragment = new QuestionArrivingFragment();
			baseFragment.setArguments(bundle);
		}
		return baseFragment;
	}

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_my_question_list, container, false);

	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {

		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {

		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		questionListAdapter = new QuestionStateAdapter(getActivity(), 2);
		toBeSolvedList.setAdapter(questionListAdapter);
		questionListAdapter.setListView(toBeSolvedList);
		custom_list.setOnAutoLoadBottomListener(new OnAutoLoadBottomListener() {

			@Override
			public void onBottomLoad() {
				getList();
			}
		});
		custom_list.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				id = "";
				getList();
			}
		});
	}

	@Override
	public void onStart() {
		id = "";
		getList();
		super.onStart();

	}

	@Override
	protected void findViews(View view) {
		resources = getActivity().getResources();
		loadingDialog = new LoadingDialog(getActivity());
		sharedPreferencesDB = SharedPreferencesDB.getInstance(getActivity());
		custom_list = (PullToCustomListView) view.findViewById(R.id.custom_list);
		toBeSolvedList = custom_list.getRefreshableView();
		left_btn = (Button) getActivity().findViewById(R.id.left_btn);
		center_btn = (Button) getActivity().findViewById(R.id.center_btn);
		center_red = (ImageView) getActivity().findViewById(R.id.center_red);
		right_btn = (Button) getActivity().findViewById(R.id.right_btn);
		right_red = (ImageView) getActivity().findViewById(R.id.right_red);
		question_bar = (RelativeLayout) view.findViewById(R.id.question_bar);
		textView1 = (TextView) view.findViewById(R.id.textView1);
		textView2 = (TextView) view.findViewById(R.id.textView2);
		tv_answer = (TextView) view.findViewById(R.id.tv_answer);
	}

	private void getList() {
		HttpMethod requestMethod = HttpMethod.GET;
		String requestUrl = URL + ASK_DOCTOR_MYQUESTIONS + "?" + DOCTORID + "=" + SharedPreferencesDB.getInstance(getActivity()).getDocotorId()
				+ "&status=1&size=10&id=" + id;
		LogUtils.e("requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(new JsonParser(), QuestionAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<QuestionAction> responseInfo) {
				loadingDialog.close();
				if (StringUtil.isEmpty(id)) {
					custom_list.onRefreshDownComplete();
				} else {
					custom_list.onLoadBottomComplete();
				}
				if (responseInfo.success.success) {
					QuestionBeen questionBeen = responseInfo.success.data;
					if (StringUtil.isEmpty(questionBeen.notReply) || questionBeen.notReply.equals("0")) {
						left_btn.setText(resources.getString(R.string.not_replay));
					} else {
						left_btn.setText(resources.getString(R.string.not_replay) + "(" + questionBeen.notReply + ")");
					}

					if (StringUtil.isEmpty(questionBeen.reply) || questionBeen.reply.equals("0")) {
						center_btn.setText(resources.getString(R.string.arriving));
					} else {
						if (questionBeen.needToReply) {
							center_red.setVisibility(View.VISIBLE);
						} else {
							center_red.setVisibility(View.GONE);
						}
						center_btn.setText(resources.getString(R.string.arriving) + "(" + questionBeen.reply + ")");
					}

					if (StringUtil.isEmpty(questionBeen.resolved) || questionBeen.resolved.equals("0")) {
						right_btn.setText(resources.getString(R.string.close));
					} else {
						sharedPreferencesDB.setCloseNum(questionBeen.resolved);
						if (!sharedPreferencesDB.getCloseNum().equals(questionBeen.resolved)) {
							right_red.setVisibility(View.VISIBLE);
						} else {
							right_red.setVisibility(View.GONE);
						}
						right_btn.setText(resources.getString(R.string.close) + "(" + questionBeen.resolved + ")");
					}
					if (questionBeen.questions.size() > 0) {
						if (StringUtil.isEmpty(id) && questionListAdapter != null && questionListAdapter.getCount() > 0)
							questionListAdapter.clearData();
						id = questionBeen.questions.get(questionBeen.questions.size() - 1).id;
						questionListAdapter.addListData(questionBeen.questions);
					} else if (StringUtil.isEmpty(id)) {
						questionListAdapter.clearData();
						getQuestion();
					}
				} else {
					getQuestion();
					Toast.makeText(getActivity(), responseInfo.success.message, Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				loadingDialog.close();
				if (StringUtil.isEmpty(id)) {
					custom_list.onRefreshDownComplete();
				} else {
					custom_list.onLoadBottomComplete();
				}
			}

			@Override
			public void onStart() {
				loadingDialog.show();
				super.onStart();

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		HttpRequest.send(params);
	}

	@Override
	public void onDestroy() {
		baseFragment = null;
		super.onDestroy();

	}

	/**
	 * <p>
	 * 功能 抢问题
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月26日 下午3:08:53
	 */
	void getQuestion() {
		if (questionListAdapter != null && questionListAdapter.getCount() < 1) {
			question_bar.setVisibility(View.VISIBLE);
			textView1.setText("您还没有进行中的问题哦,");
			textView2.setText("赶快去问题大厅抢答吧");
			tv_answer.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(MenuFragment.ACTION_FRAGMENT_MYQUESTIONHALLFRAGMENT);
					getActivity().sendBroadcast(intent);
				}
			});
		}
	}
}
